/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: polygon.js 8 2012-03-11 14:16:41Z cc.hgfeng@gmail.com $
 */

/**
 * OTR.Graph.Polygon
 * 多边形绘图对象
 * 
 * @class OTR.Graph.Polygon
 * @constructor
 * @param object params
 * 矩形参数类型（两参数互相冲突，如果同时存在，则将按排列顺序决定，排后者生效）
 * points:  顶点矩阵
 * sideCount: 边数（正多边形），图形高宽将被统一（以宽度为准）
 * rotate: 旋转角度
 */
OTR.Graph.Polygon = OTR.extend(OTR.Graph.Shape, {

     /**
      * 初始化对象
      * 
      * @param object params
      */
     _init: function(params) {
         this._addAttrs({points: null, sideCount: 3, rotate: 0});
         this._setName('Polygon');
         
         OTR.Graph.Rect.__super._init.call(this, params);
     },

    /**
     * 设置当前对象属性
     * 
     * @override
     * @param object params
     */
    setAttrs: function(params) {
       for (var i in params) {
           if (undefined === this._attrs[i]) {
               continue ;
           }
           
           switch (i) {
               case 'points':
                   this.setPoints(params[i]);
                   break;
               default:
                   this._attrs[i] = params[i];
                   break;
           }
       }
    },

    /**
     * 设置多边形顶点位置
     * 
     * @param array points
     * @return NTR.Graph.Polygon
     */
    setPoints: function(points) {
        if (!OTR.Util.isArray(points) || points.length < 3) {
            throw 'invalid point list';
        }
        
        var i = 0, k = points.length, pl = [];
        for ( ; i < k; i++) {
            var pair = points[i];
            if (OTR.Util.isObject(pair)) {
                if (undefined !== pair.x && undefined !== pair.y) {
                    pl.push({x: pair.x, y: pair.y});
                }
            } else if (OTR.Util.isArray(pair)) {
                if (pair.length >= 2) {
                    pl.push({x: pair[0], y: pair[1]});
                }
            }
        }
        
        this._attrs.points = pl;
    },
    
    /**
     * 获取顶点列表
     * 
     * @return Array()
     */
    getPoints: function() {
        if (!this._attrs.points && this._attrs.sideCount > 2) {
            var pl = [], i = 0, count = this._attrs.sideCount,
                d = Math.max(this._attrs.width, this._attrs.height),
                r = d / 2, // 半径
                dp = Math.PI * 2, // 2(double) pi
                ro = this._attrs.rotate,
                ws = this._attrs.width / d, // width scale
                hs = this._attrs.height / d, // height scale
                center = {x: this._attrs.x + r * ws, y: this._attrs.y + r * hs}; 
            
            for (; i < count; i++) {
                pl.push({
                    x: center.x + (r * Math.sin(i / count * dp + this._angelToRadian(ro))) * ws,
                    y: center.y + (r * Math.cos(i / count * dp + this._angelToRadian(ro))) * hs
                });
            }
            
            this._attrs.points = pl;
        }
        
        return this._attrs.points;
    },
    
    /**
     * 角度转换为弧度
     * 
     * @return number angel
     * @return number
     */
    _angelToRadian: function(angel) {
        return angel / 180 * Math.PI;
    }
});